import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { Http } from '@angular/http';
import { InitService } from '../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { appKeywordUrl } from '../../keyword.url';
import { keyword } from '../../keyword.data';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { DatePipe } from '@angular/common';
import { Service } from 'app/server/server';
import {appSynonymUrl} from '../../../synonym/synonym.url';

declare var $: any;
@Component({
  selector: 'app-trial-export',
  templateUrl: './trial-export.component.html'
})
export class TrialExportComponent implements OnInit {

  @Output() export = new EventEmitter<void>();
  exportThemeType: any = 'xml';
  exportPathList: any;
  exportPathCode: any = "bab111ba2b57497eb176117641c3bf1b";
  loading: boolean;
  tabListVal : Array<Object>;
  ttId:any = '';

  constructor(
    private server: Service,
    public initService: InitService,
    public removeTableService: RemoveTableService,
    private hintsService: HintsService,
    private request: CudTableService,
    private datePipe: DatePipe,
    private http: Http,
  ) { }

  ngOnInit() {
    this.server.onExportKeyworkEmit.subscribe((value: any) => {
      //this.getExportPathList();
      this._themeTabList();
    });
  }

  /**
   * 导出主题词
   * TODO::未完成
   */
  _export(info: any) {
    this.loading = true;
    info.token = keyword.retrieval.token;
    info.createBy = this.server.getuserName();
    info.createTime = this.datePipe.transform(new Date(), 'yyyy-MM-dd HH:mm:ss');
    info.themeWordsId = "";//this.removeTableService.getRemoveTableId;
    info.exportType = 'xml';//info.exportType;
    this.request
      .postRequest(this.server.baseUrl +appKeywordUrl.exportThemeWords, info)
      .then(res => {
        if (res.result_code === "0000") {
          this.export.emit();
          this.hintsService.messages('发布成功！', res.result_code);
        }else{
          this.hintsService.messages('发布失败！', res.result_code);
        }
        this.loading = false;
        
        this._init();
        return;
      })
      .catch(err => {
        this.hintsService.messages("error", "error");
        this._init();
        return;
      });
  }

  /**
   * 导出路径列表
   */
  getExportPathList() {
    let data = this._tableData();
    this.request.postRequest(this.server.baseUrl +appKeywordUrl.getExportPathList, data).then((res: any) => {
      if (res.result_code === '0000') {
        this.exportPathList = res.result;
      } else {
        this.hintsService.messages(res.message, res.result_code);
      }
      return;
    })
      .catch((err: any) => {
        this.hintsService.messages("error", "error");
        return;
      });
  }

  //查询参数
  private _tableData() {
    return {
      token: keyword.retrieval.token,
      pageNo: 1,
      pageSize: 200,
    }
  }

  /**
    * 初始化
    */
  private _init() {
    this.initService._init();
    $("#keywordExport").modal("hide");
  }

  /**
   * 获取主题词表
   */
  _themeTabList(){
    let data = {
      token         : keyword.retrieval.token,
      pageNo        : 1,
      pageSize      : 999,
    };
    this.request.postRequest(this.server.baseUrl +appKeywordUrl.getThemeTablesList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.tabListVal = res.result;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      return;
    });
  }

}
